//
//  TopSearchNavView.m
//  HuaXin
//
//  Created by 江伟 on 2018/11/1.
//

#import "TopSearchNavBtnView.h"

@interface TopSearchNavBtnView()
@property (nonatomic,strong) UIView *searchContainerView;
@property (nonatomic,strong) UILabel *searchLab;
@property (nonatomic,strong) UIImageView *searchImgView;
@property (nonatomic,strong) UIImage *leftImg;
@property (nonatomic,strong) UIImage *rightImg;
@property (nonatomic,strong) UIImage *searchImg;
@end

@implementation TopSearchNavBtnView

- (instancetype)initWithFrame:(CGRect)frame leftImg:(UIImage *)leftImg searchImg:(UIImage *)searchImg searchPlaceholder:(NSString *)str rightImg:(UIImage *)rightImg {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
        _leftImg = leftImg;
        _searchImg = searchImg;
        _rightImg = rightImg;
        
        _searchContainerView = [UIView new];
        _searchContainerView.backgroundColor = [UIColor colorWithHexString:@"#f4f4f4"];
        [self addSubview:_searchContainerView];
        ViewRadius(_searchContainerView, 3);
        
        _searchLab = [UILabel new];
        [_searchContainerView addSubview:_searchLab];
        _searchLab.textColor = [UIColor whiteColor];
        _searchLab.font = R_Font(12);
        _searchLab.text = str;
        
        _searchImgView = [[UIImageView alloc] initWithImage:searchImg];
        [_searchContainerView addSubview:_searchImgView];
        
        _searchBtn = [UIButton setupBtnWithBackgroundColor:[UIColor clearColor] title:nil titleColor:nil isBkImg:NO image:nil target:self selector:nil];
        [self addSubview:_searchBtn];
        
        _rightBtn = [UIButton setupBtnWithBackgroundColor:nil title:nil titleColor:nil isBkImg:NO image:rightImg target:nil selector:nil];
        [self addSubview:_rightBtn];
        if (leftImg) {
            
        } else {
            _searchContainerView.frame = CGRectMake(aufont(13), StatusBar_Height + 2, aufont(298), 30);
            _searchBtn.frame = _searchContainerView.frame;
            _searchImgView.frame = CGRectMake(aufont(10), 9, 12, 12);
            _searchLab.frame = CGRectMake(aufont(26), 0, aufont(260), 30);
            _rightBtn.frame = CGRectMake(_searchContainerView.right, StatusBar_Height + 2, aufont(48), 30);
        }
    }
    return self;
}

- (void)setupViews {
    
}

- (void)moveTopNavViewAlphaOffset:(CGFloat)offset {
    if (offset < 0) {
        return;
    }
    CGFloat alpha = offset / 100.0;
    if (alpha >= 1) {
        self.backgroundColor = [UIColor whiteColor];
        self.searchLab.textColor = [UIColor colorWithHexString:@"#9f9f9f"];
        self.searchContainerView.backgroundColor = [UIColor colorWithHexString:@"#f4f4f4"];
        self.searchImgView.image = IMG(@"search_gray");
        [self.rightBtn setImage:IMG(@"message_blue") forState:UIControlStateNormal];
    } else if (alpha <= 0) {
        self.searchLab.textColor = [UIColor whiteColor];
        self.searchContainerView.backgroundColor = [[UIColor colorWithHexString:@"#ffffff"] colorWithAlphaComponent:0.3];
        self.searchImgView.image = _searchImg;
        [self.rightBtn setImage:_rightImg forState:UIControlStateNormal];
    } else {
        self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:alpha];
    }
}
@end
